﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.Filters;

namespace CleanLite.Infrastructure.Configuration
{
    public static class SwaggerConfig
    {
        public static IServiceCollection AddSwaggerConfiguration(this IServiceCollection services)
        {
            return services.AddSwaggerGen(options =>
            {
                options.OperationFilter<AddResponseHeadersFilter>();
                options.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                options.OperationFilter<SecurityRequirementsOperationFilter>();
                options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Type = SecuritySchemeType.ApiKey,//安全方案/类型(模式)APIkey
                    Description = "Jwt授权(数据在请求头中进行传输) \n 请输入 Bearer {你的token(无需加括号,Bearer+空格+Token)}",
                    In = ParameterLocation.Header,//In Api密钥的位置(即通过什么传输的——头部传输)
                    Name = "JwtAuthoriza"
                });

                options.SwaggerDoc("v1", new OpenApiInfo() { Title = "Dynamic WebApi", Version = "v1" });

                options.DocInclusionPredicate((docName, description) => true);

                var basePath = AppDomain.CurrentDomain.BaseDirectory;
                var xmlPath = Path.Combine(basePath, "CleanLite.xml");
                options.IncludeXmlComments(xmlPath);
            });
        }
    }
}